// 操作数库的对象  db
// 数据库：blog_database
// 数据表：pre_note

// SELECT  字段
// FROM  数据表
// WHERE  条件
// INSERT  INTO 插入
// INTO   数据表
// UPDATE   数据表  更新
// DELETE   删除
// DELETE FROM 数据表

const db = require("../common/db/index");

module.exports = {
    // 发表随笔
    publish(note_text, createAt) {
        return new Promise(async (resolve, reject) => {
            const sql = "select note_text from pre_note where note_text = ?";
            const rows = await db.query(sql, [note_text]);
            if (rows.length == 0) {
                const sql2 = "insert into pre_note (note_text,createAt) values (?,?)";
                await db.query(sql2, [note_text, createAt]).then(
                    (result) => {
                        resolve({
                            code: 200,
                            msg: "发表随笔成功",
                            result,
                        });
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: "发表随笔失败",
                            err,
                        })
                    }
                )
            }
            else {
                reject({
                    code: -1,
                    msg: "该随笔已存在",
                })
            }
        })
    },

    // 修改随笔
    modify(note_text, createAt, id) {
        return new Promise((resolve, reject) => {
            const sql = "update pre_note  set note_text=? , createAt=? where id=?";
            db.query(sql, [note_text, createAt, id])
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "修改随笔成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "修改随笔失败",
                            err
                        })
                    }
                )
        })
    },
    // 删除随笔
    remove(id) {
        return new Promise((resolve, reject) => {
            let sql = "DELETE FROM pre_note WHERE id = ?";
            db.query(sql, [id])
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "删除随笔成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "删除随笔失败",
                            err
                        })
                    }
                )
        })
    },

    // 获取随笔列表
    list(page_num, page_size) {
        return new Promise(async (resolve, reject) => {
            const rows = await db.query(`SELECT count(*) as total FROM pre_note`);
            let page_num2 = ((parseInt(page_num) - 1) * parseInt(page_size))
            let page_size2 = (parseInt(page_size))
            let sql = `select * from pre_note order by createAt DESC limit ${page_num2} , ${page_size2}`
            await db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            total: rows[0].total,
                            msg: "获取随笔列表成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "获取随笔列表失败",
                            err

                        })
                    }
                )
        })
    },

    // 搜索随笔
    search(keyword) {
        return new Promise(async (resolve, reject) => {
            let sql = `select * from pre_note where note_text like ?`
            await db.query(sql, [`%${keyword}%`])
                .then(
                    result => {
                        if (result.length > 0) {
                            resolve({
                                code: 200,
                                msg: "搜索随笔成功",
                                result
                            })
                        } else {
                            resolve({
                                code: -1,
                                msg: "暂无该随笔",
                                result
                            })
                        }
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "搜索随笔失败",
                            err
                        })
                    }
                )
        })
    }
};
